package com.lzwk.app.function;

import com.alibaba.fastjson.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @Author: CC
 * @description url工具类
 * @Date: 2022/2/7 16:29
 */
public class UrlUtil {

    /**
     * 解析url参数
     *
     * @param frUrl
     * @return
     */
    public static JSONObject parseGetParam(String frUrl) throws UnsupportedEncodingException {
        if (frUrl == null || frUrl.equals("")) {
            return new JSONObject();
        }
        JSONObject resultJson = new JSONObject();
        String[] params = frUrl.split("&");
        for (String param : params) {
            String[] keyValue = param.split("=");
            if (keyValue.length == 1) {
                resultJson.put(keyValue[0], "");
            } else {
                String key = keyValue[0];
                String value = URLDecoder.decode(keyValue[1], "utf-8");
                resultJson.put(key, value);
            }
        }
        return resultJson;
    }

    /**
     * 解析url
     *
     * @param log
     * @return
     */
    public static JSONObject parse(String log) throws UnsupportedEncodingException, MalformedURLException, ParseException {
        JSONObject jsonObjectLog = JSONObject.parseObject(log);
        String ts = DateUtil.dateFormat(jsonObjectLog.getString("ts"));
        String dt = ts.substring(0, 10);
        String hr = ts.substring(11, 13);
        jsonObjectLog.put("ts", ts);
        jsonObjectLog.put("dt", dt);
        jsonObjectLog.put("hr", hr);
        String url = jsonObjectLog.getString("url");
        if (url == null) {
            return jsonObjectLog;
        }
        String[] urlSplit = url.split(" ");
        //没有参数
        if (urlSplit.length == 1) {
            return jsonObjectLog;
        }

        String[] urlParts = urlSplit[1].split("\\?");
        //没有参数
        if (urlParts.length == 1) {
            return jsonObjectLog;
        }
        //有参数
        String[] params = urlParts[1].split("&");
        for (String param : params) {
            String[] keyValue = param.split("=");
            if (keyValue.length == 1) {
                jsonObjectLog.put(keyValue[0], "");
            } else {
                String key = keyValue[0];
                String value = keyValue[1];
                if (key.equals("evt_params")) {
                    jsonObjectLog.put(key, JSONObject.parseObject(URLDecoder.decode(value, "utf-8")));
                } else if (key.equals("spm")) {
                    jsonObjectLog.put(key, value);
                    String[] split = value.split("\\.");
                    for (int i = 0; i < split.length; i++) {
                        jsonObjectLog.put("spm" + i, split[i]);
                    }
                } else {
                    jsonObjectLog.put(key, value);
                }
            }
        }

        String frUrl = jsonObjectLog.getString("fr_url");
        String urlReturn = "";
        String urlPat = "";
        JSONObject urlParams = new JSONObject();
        if (frUrl != null && !frUrl.equals("")) {
            URL url1 = new URL(frUrl);
            urlReturn = url1.getPath();
            urlPat = urlReturn.replaceAll("\\d+", "\\:number");
            urlParams = parseGetParam(url1.getQuery());
        }

        jsonObjectLog.put("url", urlReturn);
        jsonObjectLog.put("url_pat", urlPat);
        jsonObjectLog.put("url_params", urlParams);

        return jsonObjectLog;
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) throws UnsupportedEncodingException, MalformedURLException, ParseException {
        String url = "{\"ts1\": \"24/Feb/2022:08:01:13 +0000\", \"ua\": \"Mozilla/5.0 (iPhone; CPU iPhone OS 12_5_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.18(0x18001224) NetType/WIFI Language/zh_CN\", \"clinetip\": \"106.55.13.199\", \"forwardip\": \"\", \"url1\": \"GET/collect?tor_id=NZBIcDZGes&sid=NZBIcDZGes&ds=weike&uid=122230767&t=1643355102744&spm=wk.lp001a.df0.d00a&oid=&evt=classroom_stat_online&rts=1643355103&evt_params=%7B%22uv_id%22%3A%222640%22%2C%22session_id%22%3A%222640%22%2C%22lecture_id%22%3A25495678%2C%22account_id%22%3A122230767%2C%22props_account_id%22%3A122230767%2C%22lzwk_id%22%3A%22122230767%22%2C%22v%22%3A111%2C%22source%22%3A%22lp%22%7D&ip=183.197.7.22HTTP/1.1\", \"status\": \"200\", \"size\": \"0\", \"fr_url\": \"https://wxc507b24f938471df.weike.goweike.net/ocpx/185?source_id=11a802846b74ab91&type=source&callback=https%3A%2F%2Fad.ximalaya.com%2Fad-action%3Fuid%3D98397867%26ip%3D42.59.127.101%26os%3DANDROID%26imei_md5%3D5d9389e5b432b299bbe5849ed32f5cad%26oaid%3D18A3981DF75B4EC28A90E6915BE0A15C130a036382e558f094ddeb63aff583aa%26androidid%3D44f21efd068ea9f2%26materialid%3D2414393%26idfa%3D_IDFA_%26type%3DJS_API%26track_type%3D3%26responseid%3D8236804809097&oaid=18A3981DF75B4EC28A90E6915BE0A15C130a036382e558f094ddeb63aff583aa&idfa=_IDFA_\", \"req_time\": \"0.000\"}";

        System.out.println(parse(url).toJSONString());
    }
}
